﻿
namespace Numbrella.Tracker
{
    using System;        
    using System.IdentityModel.Selectors;
    using System.Linq;    
    using System.ServiceModel;    
    using Numbrella.Core;
    using Numbrella.Tracker.Data;    

    internal class CustomUserNameValidator : UserNamePasswordValidator
    {       
        public override void Validate(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName))
                throw new ArgumentNullException("userName");
            if (String.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            using (TrackerEntities db = new TrackerEntities())
            {                
                var users = from u in db.Users
                            where u.Name == userName
                            select u;

                if (users.Count() == 0 || users.First().Password != Utils.GetHashCode(password))
                    throw new FaultException("Unknown Username or Incorrect Password");
            }            
        }        
    }
}